home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
Boxer
/
TZUtils
/
makedb.c
< prev
next >
Wrap
Text File
|
2000-06-23
|
5KB
|
209 lines
#define IGNORE_STDIO_STUBS
#define __string_h
#ifdef OLDGCC
#include <Common.h>
#include <System/SysAll.h>
#include <UI/UIAll.h>
#include <Unix/sys_types.h>
#else
#include <PalmOS.h>
#include <PalmCompatibility.h>
#include <Unix/sys_types.h>
#endif
#include "stringil.h"
#include "stdio2.h"
struct prchead {
char name[32]; //0-32
short int attr; //32-33
short int vers; //34-35
long int cr, md, bkt; //times 36-47
long int mn, app, sort; // zero 48-59 - zero for prcs.
long int type, crea; //60-67
long int uidseed, nxrec; //68-75 - uidseed rand, nxrec zero;
short int nrecs; //76-78
} head;
struct rsrcdbent {
long int rsrc;
short int rsid;
long int ofst; //80+10*(nrecs+index)
} *rsrcent;
struct normdbent {
long int ofst, uid; //80+8*(nrecs+index)
} *dataent;
int MakeDatabase(FILE * fd)
{
LocalID lid, aiid, siid;
int i;
UInt16 cardno = 0;
UInt16 num, attr;
UInt32 uid, asz, ssz, ofst, count, total;
DmOpenRef db;
void *ap;
char buf[8];
RectangleType r;
r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
WinEraseRectangle(&r, 0);
r.topLeft.y = 30, r.extent.y = 15;
FileTell(fd, &total, NULL);
FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
memset(&head, 0, sizeof(head));
fread(&head, sizeof(head), 1, fd);
count = sizeof(head);
r.extent.x = 160 * count / total;
WinDrawRectangle(&r, 0);
WinDrawChars(head.name, strlen(head.name), 0, 15);
if( DmCreateDatabase(cardno, head.name, head.crea, head.type,
(head.attr & dmHdrAttrResDB)) ) {
lid = DmFindDatabase(cardno, head.name);
if (lid)
DmDeleteDatabase(cardno, lid);
DmCreateDatabase(cardno, head.name, head.crea, head.type,
(head.attr & dmHdrAttrResDB)) ;
}
lid = DmFindDatabase(cardno, head.name);
if (!lid) {
fclose(fd);
return 1;
}
head.attr &= ~dmHdrAttrReadOnly;
DmSetDatabaseInfo(cardno, lid, NULL, &head.attr, &head.vers, &head.cr,
&head.md, &head.bkt, &head.mn, NULL, NULL, NULL, NULL);
WinDrawChars("/", 1, 150, 0);
if (head.attr & dmHdrAttrResDB) {
rsrcent = MemPtrNew(10 * (head.nrecs + 1));
fread(rsrcent, 10, head.nrecs, fd);
count += head.nrecs * 10;
rsrcent[head.nrecs].ofst = total;
ofst = rsrcent[0].ofst;
for (i = 0; i < head.nrecs; i++)
if (rsrcent[i].ofst > rsrcent[i + 1].ofst)
break;
} else {
dataent = MemPtrNew(8 * (head.nrecs + 1));
fread(dataent, 8, head.nrecs, fd);
count += head.nrecs * 8;
dataent[head.nrecs].ofst = total;
ofst = dataent[0].ofst;
for (i = 0; i < head.nrecs; i++)
if (dataent[i].ofst > dataent[i + 1].ofst)
break;
}
r.extent.x = 160 * count / total;
WinDrawRectangle(&r, 0);
// VALIDATE
if (i != head.nrecs) {
DmDeleteDatabase(cardno, lid);
return 1;
}
db = DmOpenDatabase(cardno, lid, dmModeReadWrite);
asz = 0;
ssz = 0;
if (head.app)
asz = (head.sort ? head.sort : ofst) - head.app;
if (head.sort)
ssz = ofst - head.sort;
#define dmfread(bufP, objSize, numObj, stream) \
FileDmRead((stream), bufP, 0, (objSize), (numObj), NULL)
// fread(buf, 2, 1, fd);
WinDrawChars("\\", 1, 155, 0);
if (asz) {
while (count < head.app)
count += fread(buf, 1 , head.app - count > 8 ? 8 : head.app - count, fd);
ap = DmNewHandle(db, asz);
count += dmfread(MemHandleLock(ap), 1, asz, fd);
MemHandleUnlock(ap);
aiid = MemHandleToLocalID(ap);
DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&aiid, NULL, NULL, NULL);
}
if (ssz) {
while (count < head.sort)
count += fread(buf, 1, head.sort - count > 8 ? 8 : head.sort - count, fd);
ap = DmNewHandle(db, ssz);
count += dmfread(MemHandleLock(ap), 1, ssz, fd);
MemHandleUnlock(ap);
siid = MemHandleToLocalID(ap);
DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, &siid, NULL, NULL);
}
r.extent.x = 160 * count / total;
WinDrawRectangle(&r, 0);
if (head.attr & dmHdrAttrResDB) {
for (i = 0; i < head.nrecs; i++) {
ssz = rsrcent[i + 1].ofst - rsrcent[i].ofst;
ap = DmNewResource(db, rsrcent[i].rsrc, rsrcent[i].rsid, ssz);
while (count < rsrcent[i].ofst)
count += fread(buf, 1, rsrcent[i].ofst - count > 8 ? 8 : rsrcent[i].ofst - count, fd);
count += dmfread(MemHandleLock(ap), 1, ssz, fd);
MemHandleUnlock(ap);
DmReleaseResource(ap);
r.extent.x = 160 * count / total;
WinDrawRectangle(&r, 0);
}
MemPtrFree(rsrcent);
} else {
for (i = 0; i < head.nrecs; i++) {
ssz = dataent[i + 1].ofst - dataent[i].ofst;
num = 0xffff;
ap = DmNewRecord(db, &num, ssz);
while (count < dataent[i].ofst)
count += fread(buf, 1, dataent[i].ofst - count > 8 ? 8 : dataent[i].ofst - count, fd);
count += dmfread(MemHandleLock(ap), 1, ssz, fd);
MemHandleUnlock(ap);
attr = dataent[i].uid >> 24;
uid = dataent[i].uid & 0xffffffUL;
DmSetRecordInfo(db, num, &attr, &uid);
DmReleaseRecord(db, num, false);
r.extent.x = 160 * count / total;
WinDrawRectangle(&r, 0);
}
MemPtrFree(dataent);
}
DmCloseDatabase(db);
return 0;
}